#include "plccontrolcmd.h"

PLCControlCmd::PLCControlCmd() {}

void PLCControlCmd::SetCmd(QString cmd)
{
    this->cmd = cmd;
}

void PLCControlCmd::SetContent(QString content)
{
    this->content = content;
}

const QString &PLCControlCmd::GetCmd() const
{
    return cmd;
}

const QString &PLCControlCmd::GetContent() const
{
    return content;
}

PLCControlCmd &PLCControlCmd::operator=(PLCControlCmd &other)
{
    this->SetCmd(other.GetCmd());
    this->SetContent(other.GetContent());
    return *this;
}

QString PLCControlCmd::toJson() const
{
    QJsonObject ccObject;
    ccObject.insert(QStringLiteral("cmd"), cmd);
    ccObject.insert(QStringLiteral("content"), content);
    return QString(QJsonDocument(ccObject).toJson(QJsonDocument::Compact));
}

bool PLCControlCmd::FromJson(QString jsonStr)
{
    QJsonParseError result;
    QJsonDocument plccontrolcmdObjectRoot = QJsonDocument::fromJson(jsonStr.toUtf8().data(),&result);
    if (result.error != QJsonParseError::NoError) {
        return false;
    }

    QJsonObject plccontrolcmdObject = plccontrolcmdObjectRoot.object();
    SetCmd(plccontrolcmdObject.value("cmd").toString());
    SetContent(plccontrolcmdObject.value("content").toString());
    return true;
}

bool PLCControlCmd::FromJson(QJsonObject jsonObject)
{
    SetCmd(jsonObject.value("cmd").toString());
    SetContent(jsonObject.value("content").toString());
    return true;
}
